// pages/amountEssay/amountEssay.js
let wxparse = require("../../wxParse/wxParse.js")
let http = require('../../utils/api.js');
let domain = require('../../utils/api.js');
import {
    myHttps
} from '../../utils/util.js';
import {
    myHttpss
} from '../../utils/json.js';
Page({

    /**
     * 页面的初始数据
     */
    data: {
        id: '', //文章id
        themeList: '', //获取到得所有主题数据
        hiddenLoading: false,
        domain: '', //ip
        page:1,
        size:10
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

        this.setData({
            id: options.id,
            domain: domain.domain
        })
        this.getList(this.data.size, this.data.page); //调用文章匹配主题列表
    },
    // 页面返回的时候 刷新页面
    onShow () {
        this.getList(this.data.size, this.data.page); 
    },
    //下拉刷新从新初始化页码,条数
    onPullDownRefresh: function () {
        //下拉三个点消失
        wx.stopPullDownRefresh();
        this.setData({
            themeList: [],
            size: 10,
            page: 1,
            hiddenLoading: false
        })
        this.getList(this.data.size, this.data.page);
    },
    // 上拉加载数据
    onReachBottom: function () {
        this.setData({
            size: 10,
            page: this.data.page + 1,
        })
        this.getList(this.data.size, this.data.page);
    },
    //   获取文章匹配主题列表
    getList(size,page) {
        var url = http.api.amountEssay + this.data.id + '/topics?size='+size + '&page=' + page;
        myHttps({
            url: url,
            method: "GET",
        }).then(res => {
            if (res.code == 1) {
                this.setData({
                    themeList: res.data.content,
                    hiddenLoading: true
                })
            }
        }).catch(err => { })
    },
    //添加关注 关注主题接口
    addgz(e) {
        this.setData({
            hiddenLoading: false
        })
        var id = e.currentTarget.dataset.id;
        let index = e.currentTarget.dataset.index
        // console.log(id)
        let url = http.api.followtheme + id + '/fav';
        myHttps({
            url: url,
            method: "POST",
        }).then(res => {
            if (res.code == 1) {
                this.data.themeList[index].isFav = true,
                this.data.themeList[index].fav_count = this.data.themeList[index].fav_count + 1,
                this.setData({
                    themeList: this.data.themeList,
                    hiddenLoading: true
                })
            }
        }).catch(err => { })
    },
    // 取消关注
    canclegz(e) {
        var id = e.currentTarget.dataset.id;
        let index = e.currentTarget.dataset.index
        let url = http.api.delFollow + id + '/fav';
        myHttps({
            url: url,
            method: "DELETE"
        }).then(res => {
            if (res.code == 1) {
                this.data.themeList[index].isFav = false,
                this.data.themeList[index].fav_count = this.data.themeList[index].fav_count - 1,
                this.setData({
                    themeList: this.data.themeList,
                    hiddenLoading: true
                })
            }
        }).catch(err => { })
    },
    // 点击每一条跳转到主题主页
    goThemeHome(e) {
        let id = e.currentTarget.dataset.id
        // console.log(id)
        wx.navigateTo({
            url: '../themeHome/themeHome?id=' + id,
            success: function (res) { },
            fail: function (res) { },
            complete: function (res) { },
        })
    },
})